<?php
namespace admin\module\member;
use inc\BaseAjax\BaseAjax;
require('../../../global.php');

class ajax extends BaseAjax
{
    
    /**
     * 列出所有的成绩
     * @Author : bapijun
     * param search 搜索的名称 sort 排序方向 asc正向排序 desc逆向排序
     * echo 根据搜索参数返回符合条件的部门或者职位数组
     */
    protected function listTable()
    {
        global $db;
        $sql = 'SELECT * FROM (SELECT * FROM tb_test_result ORDER BY createtime DESC)  test';
        $count_sql = 'SELECT COUNT(tbid) FROM tb_test_result';
           if (checkVar($_GET['search'])) {
            
           
            $member = $db->select('tb_member', 'tbid', array('nickname' => $_GET['search']));
            if (!empty($member)) {
               $sql .= " WHERE member_id in ('" . implode("','", $member) . "')";
               $count_sql .= " WHERE member_id in ('" . implode("','", $member) . "')";
            } else {
                $sql .= " WHERE name = " . $_GET['search'] . ' OR mobile = ' . $_GET['search'];
               $count_sql .= " WHERE name = " . $_GET['search'] . ' OR mobile = ' . $_GET['search'];
            }
            
        }

        $sql .= ' GROUP BY  member_id ORDER BY tbid DESC'; $count_sql .= ' GROUP BY  member_id ORDER BY tbid DESC';
       
        $countResult = $db->query($count_sql)->fetchAll();
        $echo['total'] = count($countResult);
       
        $sql .= ' LIMIT ' . $_GET['limit'] . ' OFFSET ' . $_GET['offset'];
        $echo['rows'] = array();
       
        $row = $db->query($sql)->fetchAll();
        
        foreach ($row as $value1) {
            $value = $db->get('tb_member', '*', array('tbid' => $value1['member_id']));
            $img = 'dofiles/' .$value['tbid'] . '/head/original_head.jpg';

            $avatar = file_exists($img) ? $img : 'admin/img/avatar.jpg';
            $tmp['preview'] = '<img src="'.SITEROOTURL.$avatar.'" width="64" height="64" />';
            $tmp['nickname'] = $value['nickname'];
            $tmp['realname'] = $value1['name'];
            $tmp['mobile'] = $value1['mobile'];
            $tmp['child_sex'] = $value1['child_sex']==1?'男':'女';
            $tmp['child_age'] = $value1['child_age'];
            $testScoreWhere['ORDER']['tbid'] = 'DESC';
            $testScoreWhere['AND']['member_id'] = $value['tbid'];
            $testScore = $db->get('tb_test_result' , '*', $testScoreWhere);
            $tmp['createtime'] = $testScore['createtime'];
            $testScore = explode('|', $testScore['score']);
            $category = $db->select('tb_question_category' , '*', array());
            $i = 0;
            
            foreach ($testScore as $score) {
                $tmp['category' . $i] = $score;
                $i++;
            }
            
             $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs edit" data-id="'.$value['tbid'].'">查看成绩</a> ';
              $tmp['do'] .= ' <a href="javascript:;" class="btn btn-info btn-xs history" data-id="'.$value['tbid'].'">查看历史成绩</a> ';
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
        echo json_encode($echo);
    }
     /**
     * 列出历史成绩
     * @Author : bapijun
     * param search 搜索的名称 sort 排序方向 asc正向排序 desc逆向排序
     * echo 根据搜索参数返回符合条件的部门或者职位数组
     */
    protected function listHistory()
    {
        global $db;
        if (checkVar($_GET['search'])) {
            $where['OR']['createtime[~]'] = $_GET['search'];
           
        }
        if (checkVar($_GET['sort']) && checkVar($_GET['order'])) {
            $where['ORDER'][$_GET['sort']] = strtoupper($_GET['order']);
        } else {
            $where['ORDER']['tbid'] = 'DESC';
        }
        $where['AND']['member_id'] = $_GET['id'];
        $echo['total'] = $db->count('tb_test_result', '*', $where);
        $where['LIMIT'] = array($_GET['offset'], $_GET['limit']);
        $echo['rows'] = array();
        $row = $db->select('tb_test_result', '*', $where);
        foreach ($row as $value) {
            $member = $db->get('tb_member', '*', array('tbid' => $_GET['id']));
            $img = 'dofiles/' .$member['tbid'] . '/head/original_head.jpg';

            $avatar = file_exists($img) ? $img : 'admin/img/avatar.jpg';
            $tmp['preview'] = '<img src="'.SITEROOTURL.$avatar.'" width="64" height="64" />';
            $tmp['nickname'] = $member['nickname'];
            $tmp['realname'] = $value['name'];
            $tmp['mobile'] = $value['mobile'];
           $tmp['child_sex'] = $value['child_sex']==1?'男':'女';
           $tmp['child_age'] = $value['child_age'];
            $tmp['createtime'] = $value['createtime'];
            $testScore = explode('|', $value['score']);
            $category = $db->select('tb_question_category' , '*', array());
            $i = 0;
            
            foreach ($testScore as $score) {
                $tmp['category' . $i] = $score;
                $i++;
            }
            
             $tmp['do'] .= ' <a href="javascript:;" class="btn btn-primary btn-xs edit" data-id="'.$value['tbid'].'">查看成绩</a> ';
             
            $echo['rows'][] = $tmp;
            unset($tmp);
        }
        echo json_encode($echo);
    }
    /**
     * 编辑或新增员工信息
     * @Author : bapijun
     * param 员工的信息
     * echo 编辑成功返回参数 y,失败返回  参数n
     */
    protected function edit()
    {
        global $db;
        $set = array(
            'mobile' => $_POST['mobile'],
            'realname' => $_POST['realname'],
            'tel' => $_POST['tel'],
            'addtime' => $_POST['addtime'],
            'authorizelist' => $_POST['authorizelist'],
            'edittime' => date('Y-m-d H:i:s'),
        );
        if($_POST['password'] != ''){
            $set['password'] = \MyEncry::sha256($_POST['password']);
        }
        if($_POST['id'] != ''){
        
            $db->update('tb_admin', $set, array(
                'tbid' => $_POST['id']
            ));
            $cb['status'] = 'y';
        }else{
        
            $set['createtime'] = 'date()';
            $set['username'] = $_POST['username'];
            $rs = $db->insert('tb_admin', $set);
            if($rs){
                $cb['status'] = 'y';
            }else{
                $cb['status'] = 'n';
            }
        }
        echo json_encode($cb);
    }
    
    protected function delete()
    {
        global $db;
        $rs = $db->delete('tb_admin', array('tbid' => $_POST['id']));
        if($rs > 0){
            $cb['status'] = 'y';
        }else{
            $cb['status'] = 'n';
        }
        echo json_encode($cb);
    }

}

$ajax = new ajax();
$ajax->dealWithAjax();
